package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.dto.order.StatisticsOrderDTO;
import com.evil.application.pojo.dto.order.StatisticsProcessOrderDTO;
import com.evil.application.pojo.dto.order.node.OrderProcessStatisticsBO;
import com.evil.application.pojo.entity.ApplicationOrder;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.db.mapper.BatchMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 应用订单表
 *
 * @author liyang
 * @date 2023-01-30 14:34
 */
public interface ApplicationOrderMapper extends BaseMapper<ApplicationOrder>, BatchMapper<ApplicationOrder, Long> {

    int updateBatch(List<ApplicationOrder> list);

    int batchInsert(@Param("list") List<ApplicationOrder> list);

    Optional<ApplicationOrder> isExistById(Long id);

    default ApplicationOrder findById(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_ORDER_NOT_EXIST));
    }

    List<ApplicationOrder> findByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    List<ApplicationOrder> findByAppId(@Param("appId") Long appId);

    default PageInfo<ApplicationOrder> findPageByAppId(QueryModel<Long> queryModel) {
        return PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize(), queryModel.initOrder())
                .doSelectPageInfo(() -> this.findByAppId(queryModel.getParam()));
    }

    List<ApplicationOrder> findByEnterpriseId(@Param("enterpriseId") Long enterpriseId);

    default PageInfo<ApplicationOrder> findPageByEnterpriseId(QueryModel<Long> queryModel) {
        return PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize(), queryModel.initOrder())
                .doSelectPageInfo(() -> this.findByEnterpriseId(queryModel.getParam()));
    }

    /**
     * 统计订单总数
     *
     * @param statisticsOrderDTO statisticsOrderDTO
     * @return int
     */
    int countByOrderReqDTO(StatisticsOrderDTO statisticsOrderDTO);

    /**
     * 订单统计
     *
     * @param statisticsProcessOrderDTO statisticsProcessOrderDTO
     * @return List
     */
    List<OrderProcessStatisticsBO> statisticsOrder(StatisticsProcessOrderDTO statisticsProcessOrderDTO);
}